/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.amse.grigory.dchess.view;

import org.amse.grigory.dchess.kernel.*;
import javax.swing.AbstractAction;
import javax.swing.JMenuItem;
import org.amse.grigory.dchess.rules.Rules;

/**
 *
 * @author grigory
 */

public class DChessMenuItem extends JMenuItem implements IModelListener{
    public final static int BUTTON_UNDO = 1;
    public final static int BUTTON_REDO = 2;
    private int myType;
    private IModel myModel = null;

    private DChessMenuItem() {
        super();
    }

    public DChessMenuItem(AbstractAction action,int type, IModel model) {
        super(action);
        myType = type;
        myModel = model;
        switch (myType) {

            case BUTTON_UNDO:
                setText("Undo");
                setToolTipText("Undo");
                break;
            case BUTTON_REDO:
                setText("Redo");
                setToolTipText("Redo");
                break;
        }
    }

    public void update() {
        switch (myType) {

            case BUTTON_UNDO:
                if (myModel.canUndo()) {
                    setEnabled(true);
                } else {
                    setEnabled(false);
                }
                if (!myModel.isPaused()) {
                    setEnabled(false);
                }
                if (myModel.isWin() != Rules.NOBODY_WIN) {
                    setEnabled(false);
                }
                break;
            case BUTTON_REDO:
                if (myModel.canRedo()) {
                    setEnabled(true);
                } else {
                    setEnabled(false);
                }
                if (!myModel.isPaused()) {
                    setEnabled(false);
                }
                if (myModel.isWin() != Rules.NOBODY_WIN) {
                    setEnabled(false);
                }
                break;
            default:
                break;
        }



    }

}
